<?php
class SmsService
{
    private $uid;
    private $mobile;
    private $type;
    private $ip;
    private $content;
    private $appName;
    private $code;
    private $smsModel;

    function __construct($uid, $mobile, $code, $type, $ip, $content, $appName)
    {
        $this->uid              =   $uid;
        $this->mobile           =   $mobile;
        $this->code             =   $code;
        $this->type             =   $type;
        $this->ip               =   $ip;
        $this->content          =   $content;
        $this->appName          =   $appName;
        $this->smsModel         =   new SmsModel();
    }

    /**
     * 发送验证码
     */
    public function sendMobileCode()
    {
        if (empty($this->mobile) || empty($this->type) || empty($this->appName)) {
            AC::$errorNo    =   'UC001';
            return  false;
        }

        $this->checkMobileAndType();
        $this->saveSmsCodeLog();
        $this->asyncSendSms();
        if (AC::$errorNo) return false;

        return true;
    }

    /**
     * 验证手机号及类型
     */
    private function checkMobileAndType()
    {
        $isMobile   = UK::isMobile($this->mobile);
        $codeType   = $this->getMobileCodeType($this->appName, $this->type);
        if (!$isMobile || !$codeType) {
            AC::$errorNo  =   'UC020';
            return  false;
        }

        $this->type = $codeType;
        return true;
    }

    /**
     * 根据应用名获取手机短信类型
     * @param $appName
     * @param $type
     * @return mixed
     */
    private function getMobileCodeType($appName, $type)
    {
        $cnf = UK::getSmsCnf($appName);
        return $cnf['types'][$type];
    }

    /**
     * 获取短信验证通道
     * @param $appName
     * @return mixed
     */
    private function getMobileCodeExt($appName)
    {
        $cnf = UK::getSmsCnf($appName);
        return $cnf['ext'];
    }

    /**
     * 保存验证码发送记录
     */
    private function saveSmsCodeLog()
    {
        if (AC::$errorNo) return false;

        $result = $this->smsModel->addMobileCode($this->mobile, $this->type, $this->code, $this->uid, $this->ip);
        if (!$result){
            AC::$errorNo = 'UC032';
            return false;
        }

        $this->explodeMobile();
        return true;
    }

    /**
     * 组装数据，异步发送短信验证码
     */
    private function asyncSendSms()
    {
        if (AC::$errorNo) return false;

        $this->sendShortMessage(array('code' => $this->code, 'mobile' => strval($this->mobile), 'content' => $this->content, 'ext' => $this->getMobileCodeExt($this->appName)));
    }

    /**
     * 切分mobile传值，获取手机号
     */
    private function explodeMobile()
    {
        $mobileInfo = explode('-', $this->mobile);
        $this->mobile = $mobileInfo[1];
    }

    // 发短信
    private function sendShortMessage($param) {
        $data = array(
            "content"   => $param['content'],  // 短信内容
            "mobile"    => $param['mobile'],  // 手机号(18964729597)，多个用逗号分隔
            "time"      => AC::SERVER_TIME,
            "ext"       => $param['ext']
        );

        return $this->sendShortMsgJob("SendSms", $data);
    }

    // 发短信队列
    private function sendShortMsgJob($task,  $data){
        $json = UK::JsonFormat($data);
        return RedisPool::doBackgroundGearmanJob("pinksms_".$task, $json);
    }
}